package info.niwota.vi;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.text.ClipboardManager;

public final class Utils {
	private static final int VI_ID = R.string.text_voiceinput;
	private static final String VI_SAVED_DAT = "saved.dat";

	private Utils() {
	}

	public static boolean canSpeak(Context ctx) {
		PackageManager pm = ctx.getPackageManager();
		List<ResolveInfo> act = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		return (act != null && act.size() > 0);
	}

	public static void copyToClipboard(Context context, String text) {
		ClipboardManager cb = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		cb.setText(text);
	}

	public static boolean hasClipboardText(Context ctx) {
		ClipboardManager cb = (ClipboardManager) ctx
				.getSystemService(Context.CLIPBOARD_SERVICE);
		return cb.hasText();
	}

	public static String getFromClipboard(Context context) {
		ClipboardManager cb = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		boolean has = cb.hasText();
		if (!has) {
			return null;
		}
		String txt = cb.getText().toString();
		return txt;
	}

	public static void addNotification(Context ctx) {
		Notification note = new Notification(R.drawable.icon22, null, 0);
		PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx,
				VoiceInputActivity.class), 0);
		note.flags |= Notification.FLAG_NO_CLEAR;
		note.flags |= Notification.FLAG_ONGOING_EVENT;
		//
		CharSequence title = ctx.getText(R.string.app_name);
		CharSequence desc = ctx.getText(R.string.text_voiceinput_desc);
		note.setLatestEventInfo(ctx, title, desc, pi);
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.notify(VI_ID, note);
	}

	public static void removeNotification(Context ctx) {
		NotificationManager nm = (NotificationManager) ctx
				.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.cancel(VI_ID);
	}

	public static void write(Context ctx, String data) {
		FileOutputStream fos = null;
		OutputStreamWriter w = null;
		try {
			if (data == null) {
				data = "";
			}
			fos = ctx.openFileOutput(VI_SAVED_DAT, Context.MODE_PRIVATE);
			w = new OutputStreamWriter(fos);
			w.write(data);
			w.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (w != null) {
					w.close();
				}
				if (fos != null) {
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static String read(Context ctx) {
		FileInputStream fis = null;
		InputStreamReader r = null;
		BufferedReader br = null;
		try {
			fis = ctx.openFileInput(VI_SAVED_DAT);
			r = new InputStreamReader(fis);
			br = new BufferedReader(r);
			final int max = 4089;
			char[] buf = new char[max];
			int cnt = -1;
			for (int i = 0; i < max; i += cnt) {
				cnt = br.read(buf, i, (max - i));
				if (cnt == -1) {
					return new String(buf, 0, i);
				}
			}
			return new String(buf);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				}
				if (r != null) {
					r.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
